using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections.ObjectModel;

namespace planning
{
	public class Planning : ObservableObject
	{
		// ----- ATTRIBUTS -----
		private NomAnnee annee;
		private NomDivision division;
		private string promotion;
		private ICollection<Ressource> ressources = new ObservableCollection<Ressource>();
		private ICollection<Semaine> semaines = new ObservableCollection<Semaine>();

		// ----- CONSTRUCTEURS -----
		public Planning()
		{
			// Start of user code for default constructor
			// End of user code for default constructor
		}

		// ----- ACCESSEURS -----
		public NomAnnee Annee
		{
			get { return annee; }
			set { annee = value; ObjectChanged("annee"); }
		}

		public NomDivision Division
		{
			get { return division; }
			set { division = value; ObjectChanged("division"); }
		}

		public string Promotion
		{
			get { return promotion; }
			set { promotion = value; ObjectChanged("promotion"); }
		}

		public ICollection<Ressource> Ressources
		{
			get { return ressources; }
			set { ressources = value; ObjectChanged("ressources"); }
		}

		public ICollection<Semaine> Semaines
		{
			get { return semaines; }
			set { semaines = value; ObjectChanged("semaines"); }
		}


		static Dictionary<string, Planning> references = new Dictionary<string, Planning>();

		public static Planning fromXml(XmlNode node)
		{
			if(node.SelectSingleNode("@ref") != null)
				return references[node.SelectSingleNode("@ref").Value];

			Planning planning = new Planning();

			if(node.SelectSingleNode("@id") != null) {
				references.Add(node.SelectSingleNode("@id").Value, planning);
			}

			planning.Annee = (NomAnnee) Enum.Parse(typeof(NomAnnee), node.SelectSingleNode("@annee").Value);
			planning.Division = (NomDivision) Enum.Parse(typeof(NomDivision), node.SelectSingleNode("@division").Value);
			planning.Promotion = node.SelectSingleNode("@promotion").Value;
			foreach (XmlNode subElement in node.SelectNodes("ressources/*"))
			{
				Ressource subObject = Ressource.fromXml(subElement);
				planning.ressources.Add(subObject);
			}

			foreach (XmlNode subElement in node.SelectNodes("semaines/*"))
			{
				Semaine subObject = Semaine.fromXml(subElement);
				planning.semaines.Add(subObject);
			}

			

			return planning;

		}

	}

}
